package com.hu.zhcc.shiro.controller.xsx;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hu.zhcc.shiro.entity.xsx.XsxTSetmeal;
import com.hu.zhcc.shiro.entity.xsx.XsxTSetmealCheckitem;
import com.hu.zhcc.shiro.service.xsx.TSetmealCheckitemService;
import com.hu.zhcc.shiro.service.xsx.TSetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 套餐表 前端控制器
 * </p>
 *
 * @author xsx
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/tSetmeal")
public class TSetmealController {
    @Autowired
    TSetmealService tSetmealService;
    @Autowired
    TSetmealCheckitemService tSetmealCheckitemService;

    @GetMapping("/all")
    private List<XsxTSetmeal> queryAll(){
        return tSetmealService.queryAllTaoCan();
    }

    @GetMapping("/queryTaoCanStatue")
    private List<XsxTSetmeal> queryAqueryTaoCanStatuell(){
        return tSetmealService.queryTaoCanStatue();
    }

    @PutMapping("/{num}/tcid")
    private int qryy(@PathVariable("num") Integer num, Integer tcid){
        if(num==0){
            return tSetmealService.jytc(tcid);
        }
        if(num==1){
            return tSetmealService.qytc(tcid);
        }
        return 0;
    }

    @PutMapping("/{items}/taocan")
    private int add(@PathVariable("items") String items,String taocan){
        Gson gson=new Gson();
        XsxTSetmeal tSetmea =gson.fromJson(taocan, new TypeToken<XsxTSetmeal>() {}.getType());
        tSetmealService.delSetmealById(tSetmea.getId());
        tSetmealCheckitemService.delBySetmealId(tSetmea.getId());
        int i = tSetmealService.addSetmealOne(tSetmea);
        Integer tid = tSetmea.getId();//从主键返回的套餐id
        Integer[] sz =gson.fromJson(items, new TypeToken<Integer[]>() {}.getType());
        int ii=0;
        for(Integer num : sz){
            XsxTSetmealCheckitem tSetmealCheckitem=new XsxTSetmealCheckitem();
            tSetmealCheckitem.setSetmealId(tid);
            tSetmealCheckitem.setCheckitemId(num);
            tSetmealCheckitem.setCreatetime(tSetmea.getCreatetime());
            tSetmealCheckitem.setUpdatetime("");
            ii=tSetmealCheckitemService.addSetmealCheckitem(tSetmealCheckitem);
        }
        if(i>0 && ii>0){
            return 1;
        }
        return 0;
    }

}

